package com.xm.httpapi.BaseUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            int versioncode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            LogUtils.e("Exception:" + e);
        }
        return versionName;
    }

    /**
     * 返回当前程序版本号
     */
    public static int getAppVersionCode(Context context) {
        int versioncode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versioncode = pi.versionCode;
        } catch (Exception e) {
            LogUtils.e("Exception:" + e);
        }
        return versioncode;
    }

    public static boolean isMobileNO(String mobiles) {
        //第一位必定为1，第二位必定为3或4,5或8，其他位置的可以为0-9,必须为11位数

        String telRegex = "[1][3456789]\\d{9}";
        if (TextUtils.isEmpty(mobiles) && telRegex.length() != 11) {
            return false;
        } else {
            return mobiles.matches(telRegex);
        }
    }

    public static boolean isIdCard(String str){
        boolean isVerify = false;
        if(str.length()!=18){
            return isVerify;
        }
        if(str.length()==15){
            Pattern pattern = Pattern.compile("^(\\d{15})$");
            Matcher isIdCardNum = pattern.matcher(str);
            if (!isIdCardNum.matches()){
                return isVerify;
            }
            return true;
        }else{
            String str_17 = str.substring(0, 17);
            Pattern pattern = Pattern.compile("^(\\d{17})$");
            Matcher isIdCardNum = pattern.matcher(str_17);
            if (!isIdCardNum.matches()){
                return isVerify;
            }
            String str_18 = str.substring(17, 18);
            Pattern pattern_18 = Pattern.compile("^(\\d{1})$");
            Matcher isIdCardNum_18 = pattern_18.matcher(str_18);
            if (!isIdCardNum_18.matches()){
                if(!str_18.equals("x") && !str_18.equals("X")){
                    return isVerify;
                }
            }
        }
        return true;
    }

    /**
     * 隐藏软键盘
     *
     * @param context
     * @return
     */
    public static void isHideKeyborad(Context context, View view) {
        isHideKeyborad(context, 0, view);
    }

    public static void isHideKeyborad(Context context, int type, View view) {//0为隐藏 1为显示 2为隐藏显示互换
        if (view == null) {
            return;
        }
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();
        switch (type) {
            case 0:
                if (isOpen)
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                break;
            case 1:
                if (!isOpen)
                    imm.showSoftInputFromInputMethod(view.getWindowToken(), 0);
                break;
            case 2:
                imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
        }
    }

    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 截取全屏
     *
     * @return
     */
    public static Bitmap captureScreenWindow(Activity activity) {
        activity.getWindow().getDecorView().setDrawingCacheEnabled(true);
        Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();
        return bmp;
    }

    public static void Call(Context context, String phone) {
        if (TextUtils.isEmpty(phone)) {
            Toast.show( "手机号码为空");
            return;
        }
        Intent intentPhone = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intentPhone.setData(data);
        intentPhone.setAction(Intent.ACTION_DIAL);
        context.startActivity(intentPhone);
    }
}
